package com.cyl.manager.lbs.controller;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.manager.act.domain.entity.CouponActivity;
import com.cyl.manager.lbs.convert.LbsZbjlConvert;
import com.cyl.manager.lbs.domain.LbsZbjl;
import com.cyl.manager.lbs.mapper.LbsZbjlMapper;
import com.cyl.manager.lbs.service.LbsZbDpService;
import com.cyl.manager.lbs.service.LbsZbjlService;
import com.cyl.manager.oms.domain.query.DpOrderQuery;
import com.cyl.manager.oms.domain.vo.DpOrderProductVO;
import com.cyl.manager.oms.domain.vo.DpOrderVO;
import com.cyl.manager.ums.domain.entity.Member;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.framework.config.LocalDataUtil;
import com.ruoyi.livesass.LiveVideoService;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 *  直播大屏统计Controller
 *
 * @author zcc
 * @date 2025-02-09
 */
@Api(description ="直播大屏统计")
@RestController
@RequestMapping("/mall/lbsZbDp")
public class LbsZbDpController extends BaseController {
    @Autowired
    private LbsZbDpService service;
  
    @Autowired
    private LbsZbjlMapper lbsZbjlMapper;
    
    @Autowired
    private LiveVideoService liveVideoService;
    
    @ApiOperation("获取直播间信息")
    @GetMapping("/getZbjl")
    public ResponseEntity<LbsZbjl> getZbjl(Long zbjlid,String userId,String nickname){
	      if(userId==null) {
	    	 userId = "1";
	      }
	      if(nickname==null) {
	    	 nickname = "管理员";
	      }
	      LbsZbjl lbsZbjl = 	lbsZbjlMapper.selectById(zbjlid);
	  	  Map<String, Object> params = new HashMap<String, Object>();
		  long timestamp = System.currentTimeMillis();
	  	  params.put("timestamp", timestamp);
	  	  params.put("nickname", URLEncoder.encode(Base64.encode(nickname)));
	  	  params.put("userid", userId);
	  	  params.put("sign", liveVideoService.getSignDp(zbjlid,userId,timestamp));
	  	  lbsZbjl.setParams(params); 
          return ResponseEntity.ok(lbsZbjl);
    }
    

    @ApiOperation("商品成交排行榜")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:list')")
    @PostMapping("/selectDpCjph")
    public ResponseEntity<List<DpOrderProductVO>> selectDpCjph(@RequestBody DpOrderQuery dpOrderQuery) {
    	 return ResponseEntity.ok(service.selectDpCjph(dpOrderQuery));
    }
    
    @ApiOperation("成交总数")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:list')")
    @PostMapping("/selectDpCjzs")
    public ResponseEntity<Map<String, DpOrderVO>> selectDpCjzs(@RequestBody DpOrderQuery dpOrderQuery) {
    	Map<String, DpOrderVO> map = new HashMap<String, DpOrderVO>();
    	map.put("PAY", new DpOrderVO(0));
    	map.put("GMV", new DpOrderVO(0));
    	
    	List<DpOrderVO> dpOrderVO = service.selectDpCjzs(dpOrderQuery);
    	List<DpOrderVO> dpOrderGmvVO = service.selectDpGmvCjzs(dpOrderQuery);
    	if(dpOrderVO!=null && dpOrderVO.size()>0) {
    		map.put("PAY", dpOrderVO.get(0));
    	}
    	if(dpOrderGmvVO!=null && dpOrderGmvVO.size()>0) {
    		map.put("GMV", dpOrderGmvVO.get(0));
    	}
    	 return ResponseEntity.ok(map);
    }
    
    
    
    @ApiOperation("单个商品成交总数")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:list')")
    @PostMapping("/selectDpTjByProductId")
    public ResponseEntity<DpOrderVO> selectDpTjByProductId(@RequestBody DpOrderQuery dpOrderQuery) {
    		DpOrderVO dpOrderVO = new DpOrderVO(0);
        	List<DpOrderVO> list = service.selectDpTjByProductId(dpOrderQuery);
        	if(list!=null && list.size()>0) {
        		dpOrderVO = list.get(0);
        	}
    	 return ResponseEntity.ok(dpOrderVO);
    }
    
    
    @ApiOperation("5分钟一条统计数据")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:list')")
    @PostMapping("/selectDp5min")
    public ResponseEntity<List<DpOrderVO>> selectDp5min(@RequestBody DpOrderQuery dpOrderQuery) {
    	 return ResponseEntity.ok(service.selectDp5min(dpOrderQuery));
    }
    
    @ApiOperation("商品未支付统计")
    @PreAuthorize("@ss.hasPermi('mall:lbsZbjl:list')")
    @PostMapping("/selectProductWzfTj")
    public ResponseEntity<Integer> selectProductWzfTj(@RequestBody DpOrderQuery dpOrderQuery) {
    	 return ResponseEntity.ok(service.selectProductWzfTj(dpOrderQuery));
    }
  

}
